package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant;
import com.cskaoyan.wordmemorize.converter.VocCategoryConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocBookDO;
import com.cskaoyan.wordmemorize.dao.entity.VocCategoryDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocBookMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocCategoryMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteBookCategoryOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocCategoryDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.VocCategoryCommand;
import com.cskaoyan.wordmemorize.service.VocCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VocCategoryServiceImpl implements VocCategoryService {


    @Autowired
    VocCategoryMapper vocCategoryMapper;


    @Autowired
    VocCategoryConverter vocCategoryConverter;

    @Autowired
    VocBookMapper vocBookMapper;

    /**
     * 分页获取单词类目
     * @param pageQuery
     * @return
     */
    @Override
    public PageDTO<VocCategoryDTO> getBookCategories(PageRequest pageQuery) {

        if (pageQuery.getPageNum() == null) {
            // 查询所有的书籍类目
            List<VocCategoryDO> vocCategoryDOs = vocCategoryMapper.selectList(null);

            return vocCategoryConverter.bookCategoryList2Page(vocCategoryDOs, (long)vocCategoryDOs.size());
        }

        // 需要分页
        Page<VocCategoryDO> pageParam = new Page<>(pageQuery.getPageNum(), pageQuery.getCount());
        LambdaQueryWrapper<VocCategoryDO> vocCategoryWrapper = new LambdaQueryWrapper<>();
        if (pageQuery.getKeyword() != null) {
            vocCategoryWrapper.like(VocCategoryDO::getCategoryName, pageQuery.getKeyword());
        }
        Page<VocCategoryDO> vocCategoryPage = vocCategoryMapper.selectPage(pageParam, vocCategoryWrapper);


        return vocCategoryConverter.bookCategoryList2Page(vocCategoryPage.getRecords(), vocCategoryPage.getTotal());
    }


    @Override
    public VocCategoryDTO getBookCategory(Long categoryId) {

        return vocCategoryConverter.bookCategoryEntity2DTO(vocCategoryMapper.selectById(categoryId));
    }

    /**
     * 添加书籍类目
     * @param command
     * @return
     */
    @Override
    @OperateLog(dataType = "书籍分类", operateType = 1, description = "新增书籍分类")
    public Long addBookCategory(VocCategoryCommand command) {


        return (long) vocCategoryMapper.insert(vocCategoryConverter.bookCategoryCommand2Entity(command));


    }

    /**
     * 更新书籍类目
     * @param command
     */
    @Override
    @OperateLog(dataType = "书籍分类", operateType = 4, description = "修改书籍分类")
    public void updateBookCategory(VocCategoryCommand command) {

        vocCategoryMapper.updateById(vocCategoryConverter.bookCategoryCommand2Entity(command));
    }

    /**
     * 删除书籍类目
     * @param categoryId
     * @return
     */
    @Override
    @OperateLog(dataType = "书籍分类", operateType = 2, description = "删除书籍分类", logReturnValue = true)
    public DeleteBookCategoryOperateDTO deleteBookCategory(Long categoryId) {

        int row=vocCategoryMapper.deleteById(categoryId);
        if (row > 0){
            vocBookMapper.delete(new LambdaQueryWrapper<VocBookDO>().eq(VocBookDO::getBcId, categoryId));
        }
        DeleteBookCategoryOperateDTO dto = new DeleteBookCategoryOperateDTO();
        dto.setId(categoryId);
        return dto;


    }

    /**
     * 批量删除书籍类目
     * @param command
     * @return
     */
    @Override
    @OperateLog(dataType = "书籍分类", operateType = 3, description = "批量删除书籍分类", logReturnValue = true)
    public List<DeleteBookCategoryOperateDTO> deleteBookCategories(BatchDeleteCommand command) {

        List<String> ids = command.getIds();
        DeleteBookCategoryOperateDTO dto = new DeleteBookCategoryOperateDTO();
        for (int i = 0; i < ids.size(); i++) {

            int j=vocCategoryMapper.deleteById(ids.get(i));
            if (j > 0){
                vocBookMapper.delete(new LambdaQueryWrapper<VocBookDO>().eq(VocBookDO::getBcId, ids.get(i)));
                j=0;
            }
        }
        return vocCategoryConverter.bookCategoryDOsToDeleteCategoryDTOs(vocCategoryMapper.selectBatchIds(ids));

    }
}
